package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.domain.LitemallStoreTag;
import org.linlinjava.litemall.db.domain.LitemallStoreTagCategory;
import org.linlinjava.litemall.db.service.LitemallTagCategoryService;
import org.linlinjava.litemall.db.service.LitemallTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description:	标签类别管理
 * @Author:			Coach tam
 * @Company:		坚持灵活  灵活坚持
 * @CreateDate:		2021-4-11 9:29:06
 */
@RestController
@RequestMapping("/wx/tagcategory")
@Validated
public class WxTagCategoryController {
    private final Log logger = LogFactory.getLog(WxTagCategoryController.class);

    @Autowired
    private LitemallTagCategoryService tagcategoryService;

    @Autowired
    private LitemallTagService tagService;

    @GetMapping("/index")
    public Object index() {
        List<LitemallStoreTagCategory> categoryList = tagcategoryService.findAllForIndex();
        LitemallStoreTagCategory currentCategory = categoryList.get(0);
        List<LitemallStoreTag> currentSubCategory = tagService.findListByCategoryId(currentCategory.getId());


        HashMap<Object, Object> data = new HashMap<>();
        data.put("categoryList", categoryList);
        data.put("currentCategory", currentCategory);
        data.put("currentSubCategory", currentSubCategory);

        return ResponseUtil.ok(data);
    }

    /**
     * 当前分类栏目
     *
     * @param id 分类类目ID
     * @return 当前分类栏目
     */
    @GetMapping("current")
    public Object current(@NotNull Integer id) {
        // 当前分类
        LitemallStoreTagCategory currentCategory = tagcategoryService.findById(id);
        if(currentCategory == null){
            return ResponseUtil.badArgumentValue();
        }
        List<LitemallStoreTag> currentSubCategory = tagService.findListByCategoryId(currentCategory.getId());

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("currentCategory", currentCategory);
        data.put("currentSubCategory", currentSubCategory);
        return ResponseUtil.ok(data);
    }

}
